package com.bite.book.controller;

import com.bite.book.dao.BookDao;
import com.bite.book.model.*;
import com.bite.book.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.print.Book;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static com.bite.book.constants.Constant.SESSION_USER_KEY;

/*
 * * 图书展示列表:
 * 1. url:/user/getList
 * 2. 参数: 无
 * 3. 返回: 列表
 * */
//应用分层
//controller 控制层 接收前端请求, 并相应
//controller调用service
//service 业务逻辑层 处理具体的业务逻辑
//service 调用dao 数据访问层
//dao 数据访问层 对数据的增删查改
//model 存放实例对象
@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService = new BookService();
    /**
     * 根据页码来获取每页的信息
     * url: /book/getListByPage
     * 参数: PageRequest
     * @return PageResult
     */
    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(PageRequest pageRequest, HttpSession session){
        log.info("接收到参数{}", pageRequest);
        PageResult<BookInfo> result = bookService.getListByPage(pageRequest);

        return Result.success(result);
    }

    /**
     * 添加图书
     * url: /book/addBook
     *
     * @param: bookInfo
     * @return: Boolean
     */
    @RequestMapping("/addBook")
    public Boolean addBook(BookInfo bookInfo) {
        log.info("接收到参数:{}", bookInfo);
        if(!StringUtils.hasLength(bookInfo.getBookName())
        || !StringUtils.hasLength(bookInfo.getAuthor())
        || bookInfo.getCount() == null
        || bookInfo.getPrice() == null
        || !StringUtils.hasLength(bookInfo.getPublish())
        || bookInfo.getStatus() == null){
            log.error("参数不完整");
            return false;
        }
      Integer result = bookService.addBook(bookInfo);
        if(result == 0){
            log.error("添加失败");
            return false;
        }
        return true;
    }

    /**
     * 根据id拿到要修改的图书信息
     * @param id
     * @return
     */
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer id){
        if(id == null || id <= 0){
            return new BookInfo();
        }
        return bookService.queryBookById(id);
    }

    /**
     * 修改图书
     * @param bookInfo
     * @return true 修改成功 false 修改失败
     */
    @RequestMapping("/updateBook")
    public Boolean updateBook(BookInfo bookInfo){
        log.info("接收到参数: {}",bookInfo);
        if(!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() == null){
            log.error("参数不完整");
            return false;
        }
        try{
            bookService.updateBook(bookInfo);
            return true;
        }catch (Exception e){
            log.error("修改失败: {}",e.getMessage());
            return false;
        }

    }

    /**
     * 删除图书(逻辑删除)
     * @param id
     * @return 执行结果
     */
    @RequestMapping("/deleteBook")
    public Boolean deleteBook(Integer id){
        if(id == null || id <= 0){
            return false;
        }
        bookService.deleteBook(id);
        return true;
    }
@RequestMapping("/batchDeleteBook")
    public Boolean batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书, ids:{}",ids);
        try{
            bookService.batchDeleteBook(ids);
            return true;
        }catch(Exception e){
            return false;
        }
    }
}
